





const getRootDir = () => {
	// 判断系统是否是安卓
	if (uni.getSystemInfoSync().platform !== "android") {
		plus.nativeUI.alert("这个组件只能在安卓下使用")
		return
	}
	
	// 导入原生类
	let environment = plus.android.importClass("android.os.Environment");
	
	// 判断sd卡是否挂载
	if (environment.getExternalStorageState() !== environment.MEDIA_MOUNTED) {
		plus.nativeUI.alert("SD卡没有挂载")
	}
	// 获取SD卡根目录
	let sdRoot = environment.getExternalStorageDirectory()
	
	let sdRootList = plus.android.invoke(sdRoot, "listFiles")
	
	// 定义自定义数据
	let sdRootPathList = []

	// 组合数据
	sdRootList.forEach(v => {
		// 判断是否可见
		if(plus.android.invoke(v, "isHidden") == true){
			return false
		}
		let pathName = v.toString().split("/")
		if (plus.android.invoke(v, "isDirectory") === true) {
			sdRootPathList.push({
				isDirectory: true,
				path: v,
				pathName: pathName[pathName.length -1]
			})
		} else {
			sdRootPathList.push({
				isDirectory: false,
				path: v,
				pathName: pathName[pathName.length -1]
			})
		}
	})

	return sdRootPathList
}


const getSubDir = path => {
	try {
		if (plus.android.invoke(path, "isDirectory") !== true) {
			return null
		}

		let pathList = plus.android.invoke(path, "listFiles")
		// 定义自定义数据
		let subPathList = []

		// 组合数据
		pathList.forEach(v => {
			// 判断是否可见
			if(plus.android.invoke(v, "isHidden") == true){
				return false
			}
			let pathName = v.toString().split("/")
			if (plus.android.invoke(v, "isDirectory") === true) {
				subPathList.push({
					isDirectory: true,
					path: v,
					pathName: pathName[pathName.length -1]
				})
			} else {
				subPathList.push({
					isDirectory: false,
					path: v,
					pathName: pathName[pathName.length -1]
				})
			}
		})
		
		return subPathList
		
	} catch (e) {
		return null
	}

}



function loadDir() {
	let environment = plus.android.importClass("android.os.Environment");
	// 判断SD卡是否插入(涉及到SDK的读取，最好先判断SDK是否插入)
	// environment.getExternalStorageState() === environment.MEDIA_MOUNTED
	// 获得sd卡根目录
	let path = environment.getExternalStorageDirectory().getAbsolutePath();
	this.rootPath = path;
	let lastPath = uni.getStorageSync('lastPath');
	// 如果上次打开过目录，则使用上次的路径
	if(lastPath) {
		path = lastPath;
	}
	let list = this.getFiles({
		fullPath: path,
		isFile: false
	});
	return list;
}

function getFiles(file) {
	if (!file.isFile) {
		let dirs = [],
			files = [];
		let directory = new File(file.fullPath);
		let arr = directory.listFiles();
		this.path = file.fullPath;
		arr.forEach(item => {
			if (!item.isHidden()) {
				if (item.isDirectory()) {
					dirs.push({
						name: item.getName(),
						fullPath: item.getPath(),
						isFile: false,
					})
				} else {
					// 文件  
					files.push({
						name: item.getName(),
						fullPath: item.getPath(),
						isFile: true, // 是否是文件
						isAdded: this.isAdded(item.getPath()) // 是否已经添加到文件列表页
					})
				}
			}
 
		})
		return [...dirs, ...files]
	}
}

function getNextFile(e, file) {
	this.checkedAll = false;
	if (file.isFile) {
		this.$set(file, 'checked', !file.checked)
		let checkedAll = true;
		this.fileList && this.fileList.forEach(item => {
			if (!item.checked && item.isFile) {
				checkedAll = false;
			}
		});
		this.checkedAll = checkedAll;
		this.getCheckedFiles();
	} else {
		return this.getFiles(file)
	}
}



export default {
	getRootDir,
	getSubDir,
	loadDir,
	getNextFile
}
